package xq.demo.xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

// 使用XML文件存储演示配置信息，使用dom4j访问XML文件
public class ConfigUtil {
	public static void writeConfig(File file, Config config) {
		Document document = DocumentHelper.createDocument();
		Element rootElement = document.addElement("configurations");
		Element configElement = rootElement.addElement("config");
		if(config.getName()!=null)
			configElement.addAttribute("name", config.getName());
		Element minSizeElement = configElement.addElement("minSize");
		minSizeElement.addAttribute("desc", config.getMinSizeDesc());
		minSizeElement.setText(Integer.toString(config.getMinSize()));
		Element maxSizeElement = configElement.addElement("maxSize");
		maxSizeElement.addAttribute("desc", config.getMaxSizeDesc());
		maxSizeElement.setText(Integer.toString(config.getMaxSize()));
		Element writableElement = configElement.addElement("writable");
		writableElement.addAttribute("desc", config.getWritableDesc());
		writableElement.setText(Boolean.toString(config.isWritable()));
		try {
			FileWriter fileWriter = new FileWriter(file);
			OutputFormat xmlFormat = new OutputFormat();
			xmlFormat.setEncoding("UTF-8");
			xmlFormat.setNewlines(true);
			xmlFormat.setLineSeparator("\r\n");
			xmlFormat.setIndent(true);
			xmlFormat.setIndent("\t");
			// 创建写文件方法
			XMLWriter xmlWriter = new XMLWriter(fileWriter, xmlFormat);
			xmlWriter.write(document);
			xmlWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void writeConfig(File file, List<Config> configs) {
		Document document = DocumentHelper.createDocument();
		Element rootElement = document.addElement("configurations");
		for(Config config: configs) {
			Element configElement = rootElement.addElement("config");
			if(config.getName()!=null)
				configElement.addAttribute("name", config.getName());
			Element minSizeElement = configElement.addElement("minSize");
			minSizeElement.addAttribute("desc", config.getMinSizeDesc());
			minSizeElement.setText(Integer.toString(config.getMinSize()));
			Element maxSizeElement = configElement.addElement("maxSize");
			maxSizeElement.addAttribute("desc", config.getMaxSizeDesc());
			maxSizeElement.setText(Integer.toString(config.getMaxSize()));
			Element writableElement = configElement.addElement("writable");
			writableElement.addAttribute("desc", config.getWritableDesc());
			writableElement.setText(Boolean.toString(config.isWritable()));
		}
		try {
			FileWriter fileWriter = new FileWriter(file);
			OutputFormat xmlFormat = new OutputFormat();
			xmlFormat.setEncoding("UTF-8");
			xmlFormat.setNewlines(true);
			xmlFormat.setLineSeparator("\r\n");
			xmlFormat.setIndent(true);
			xmlFormat.setIndent("\t");
			// 创建写文件方法
			XMLWriter xmlWriter = new XMLWriter(fileWriter, xmlFormat);
			xmlWriter.write(document);
			xmlWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static List<Config> parseConfig(File file) {
		List<Config> cList = new ArrayList<>();
		SAXReader saxReader = new SAXReader();
		Document document;
		try {
			document = saxReader.read(new FileInputStream(file));
			Element rootElement = document.getRootElement();	// 获取根节点configutions
			List<Element> eList = rootElement.elements();
			for(Element e: eList) {
				Config config = new Config();
				Element element;
				String desc, value;
				config.setName(e.attributeValue("name"));
				element = e.element("minSize");	// 获取minSize节点
				desc = element.attributeValue("desc");
				config.setMinSizeDesc(desc);
				value = element.getText().trim();
				config.setMinSize(Integer.parseInt(value));
				element = e.element("maxSize");	// 获取maxSize节点
				desc = element.attributeValue("desc");
				config.setMaxSizeDesc(desc);
				value = element.getText().trim();
				config.setMaxSize(Integer.parseInt(value));
				element = e.element("writable");	// 获取writable节点
				desc = element.attributeValue("desc");
				config.setWritableDesc(desc);
				value = element.getText().trim();
				config.setWritable(Boolean.parseBoolean(value));
				cList.add(config);
		}
		} catch (FileNotFoundException | DocumentException e) {
		}
		return cList;
	}
}
